IF EXISTS(SELECT name FROM sysobjects
      WHERE name = 'pOutProdHistory_Get' AND type = 'P')
   DROP PROCEDURE pOutProdHistory_Get
GO

CREATE PROCEDURE dbo.pOutProdHistory_Get
	@vintProductId		INT,
	@vdatFromDate		DATETIME = NULL,
	@vdatToDate			DATETIME = NULL
AS


/*______________________ CONSTANT DECLARATIONS _________________________*/

  

/*______________________ VARIABLE DECLARATIONS _________________________*/

  
/*_______________________________ INIT _________________________________*/


/*________________________ VALIDATE PARAMETERS _________________________*/
	
/*_______________________________ MAIN _________________________________*/
	
	
	SELECT pd.Amount, 
		pp.Date,
		RowNumber = ROW_NUMBER()OVER(ORDER BY pp.Date)
	FROM OutProdDetail pd 
	
	INNER JOIN OutProdPaper pp
	ON pd.OutProdPaperId = pp.Id
	
	WHERE ProductId = @vintProductId
		AND ((@vdatFromDate IS NULL) OR (DATEDIFF(DAY, @vdatFromDate, pp.Date) >= 0))
		AND ((@vdatToDate IS NULL) OR (DATEDIFF(DAY, pp.Date, @vdatToDate) >= 0))
	
	ORDER BY pp.Date DESC

/*___________________________ ERROR HANDLER ____________________________*/



